import {all, create} from 'mathjs'
import XEUtils from 'xe-utils'


const config = { }
const math = create(all, config)
let integrate={
    transform:function (args, math, scope) {
        return args+math+scope
    },
    changeZero:function (args,def) {
        args=XEUtils.toNumber(args)
        if (args===0||args===null||args===undefined){
            return def
        }else{
            return args
        }
    },
    ifNull:function (args,def) {
        if (args===0||args===''||args===null||args===undefined){
            return null
        }else{
            return def
        }
    },
    expjs:function (args) {
        return math.evaluate(args)
    },
    exConcat:function (args){
        console.log(args);
        
        let v=''
      if (XEUtils.isArray(args._data)){
          XEUtils.arrayEach(args._data,item=>{
              v+=item
          })
          return v
      }  else {
          return args
      }
    },
    exrt:function(args:any){
        console.log(args);
        
        if(!args){
            return
        }
        let arr = args.split(',')
        console.log(arr);
        
        if(arr.length==1){
            return arr[0]
        }else{
            let sun = 0
            arr.forEach((v:any) => {
                sun=sun+v*1
            });
            return sun
        }
        // console.log(arr);
        // return 123
    }
}





math.import(integrate)
export function mathAll (val:string,data:object){
    let Data={}

   console.log(val,data)
    const regex = /(F\d+)/gm;
    let m,str=val;
    while ((m = regex.exec(val)) !== null) {
        // 这对于避免零宽度匹配的无限循环是必要的
        if (m.index === regex.lastIndex) {
            regex.lastIndex++;
        }
        str=str.replaceAll(m[0],"'"+(data[m[0]]?data[m[0]]:'')+"'")
        
    }
    
    
  return math.evaluate(str)
}